Skip to content

gh-141778: add missing validation in ast.literal_eval() for non-string input - #142969

Closed
skirpichev wants to merge 12 commits into
python:mainfrom
skirpichev:validation-of-Constants/141778
Closed

gh-141778: add missing validation in ast.literal_eval() for non-string input#142969
skirpichev wants to merge 12 commits into
python:mainfrom
skirpichev:validation-of-Constants/141778

Conversation

@skirpichev

@skirpichev skirpichev commented Dec 19, 2025

Copy link
Copy Markdown
Member

Also support sentinel in the literal_eval().

This also changes parsing of the private __text_signature__ attribute by inspect.signature(). Now we accept here only types, valid for ast.Constant().

The signature of _thread.interrupt_main() was marked as unsupported. I think it's a reasonable regression. Previously it was rendered with 2 as default. I think we should rather preserve repr for int's subclasses, like pure-Python methods do. See also #61005.

…-string input

This also changes parsing of the private `__text_signature__` attribute
by inspect.signature().  Now we accept here only types, valid for
ast.Constant().
Comment thread Lib/ast.py
_type_Ellipsis = type(...)


def _convert_literal(node, omit_validation=False):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change? It may make life harder for some users.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you be more precise, any example?

We need this, because literal_eval() accepts invalid AST for non-string input. Per documentation:

The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None and Ellipsis.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on case from stdlib (_signature_fromstr()), I think that possible cases of using this undocumented feature are rare, if exist at all.

Though, we can properly deprecate old behavior for extra safety.

@skirpichev skirpichev closed this Dec 25, 2025
@skirpichev
skirpichev deleted the validation-of-Constants/141778 branch December 25, 2025 21:29
@skirpichev
skirpichev restored the validation-of-Constants/141778 branch May 23, 2026 08:18
@skirpichev skirpichev reopened this May 23, 2026
@skirpichev
skirpichev marked this pull request as draft May 23, 2026 08:18
@skirpichev skirpichev self-assigned this May 23, 2026
@read-the-docs-community

Copy link
Copy Markdown

@skirpichev
skirpichev marked this pull request as ready for review August 18, 2026 06:35
@skirpichev

Copy link
Copy Markdown
Member Author

I can split out sentinel support to a separate pr.

@skirpichev skirpichev closed this Aug 18, 2026
@skirpichev
skirpichev deleted the validation-of-Constants/141778 branch August 18, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants